<?php
namespace Home\Model;
use Think\Model;
class IndexModel extends Model {
	
	public function financial_list($pageNo,$pageSize,$wheres)
	{    
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $where='1=1';
		 $where.=" and status = 4 and sales_return = '' ";
		 
		//业务员权限
	    if($wheres['type']=='2')
		{
			$where.=' and salesman = "'.$_SESSION['user'].'"';	
		}
		
		
		 //模糊查询订单号
	    if(!empty($wheres['order_number']))
		{
			$where.=' and order_number like "%'.$wheres['order_number'].'%"';
		}
		
		//查询是否已审核
	    if(!empty($wheres['is_finish']))
		{
			$where.=' and is_finish ='.$wheres['is_finish'];
			
		}else{
			$where.=' and is_finish =""';
		}
		
		//根据创建时间查询
		 if(!empty($wheres['delivery_time_start']))
		{	$wheres['delivery_time_start'] .= " 00:00:00";
			$where.=" and delivery_time >= '".$wheres['delivery_time_start']."'";
		}
		
		
		
		if(!empty($wheres['delivery_time_end']))
		{
			$wheres['delivery_time_end'] .= " 23:59:59";
			$where.=" and delivery_time <= '".$wheres['delivery_time_end']."'";
		}
		
		//模糊查询客户名称
	    if(!empty($wheres['customer_name']))
		{
			$where.=' and customer_name like "%'.$wheres['customer_name'].'%"';
			
		}
		
		if(!empty($wheres['salesman'])){
			$where.=' and salesman ="'.$wheres['salesman'].'"';
		}
		 $order=M('order');
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->order('delivery_time')->select();
		 $count['count']=count($order_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 return $result;
		 
	}

	//数据管理列表页
	public function information_list($pageNo,$pageSize,$wheres)
	{    
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 if($wheres['cycle']==1){
			  //$what = $wheres['end']-$wheres['start'];
			  $start = $wheres['start'];
			  $end = $wheres['end'];
			  $what = $end - $start;
			  $wheres['start'] .= "-01-01 00:00:00";
			  $wheres['end'] .= "-12-31 23:59:59";
		}else{
		 	$arr1 = explode('-',$wheres['end']);
			$arr2 = explode('-',$wheres['start']);
			$what =$arr1['1'] - $arr2['1'];
		}
		 $where='1=1';
		 $where .=" and is_finish ='1'";
		//根据订单完成时间查询
		 /*if(!empty($wheres['start']))
		{	
			if($wheres['cycle']==1){
				
				//$wheres['start'] .= "-01-01 00:00:00";
				
			}else{
				$wheres['start'] .= "-01 00:00:00";
			}
			$where.=" and order.finish_time >= '".$wheres['start']."'";
		}
		if(!empty($wheres['end']))
		{
			
			if($wheres['cycle']==1){
				$wheres['end'] .= "-12-31 23:59:59";
			}else{
				$wheres['end'] .= "-31 23:59:59";
				
			}
			$where.=" and order.finish_time <= '".$wheres['end']."'";
		}*/
		
		 //查询业务员
	    if(!empty($wheres['team_members']))
		{
			$where.=" and salesman = '".$wheres['team_members']."'";
		}
		
		 //查询业务团队
	    if(!empty($wheres['team']))
		{
			$team_list = M('team')->field('team_members')->where(array('team_name'=>$wheres['team']))->find();
			$arr = explode('|',$team_list['team_members']);
			$where.=' and (';
			foreach($arr as $k=>$v){
				if($k==0){
					$where.=' salesman = "'.$v.'"';
				}else{
					$where.=' or salesman = "'.$v.'"';
				}
			}
			$where.=')';
		}
		 
		 $information_list = array();
		 $exchange_rate = M('exchange_rate')->find();
		 
				
		 if($wheres['cycle']==2){
			//$what = $wheres['mon_end']-$wheres['mon_start'];
			
			if(empty($wheres['mon_end']) && empty($wheres['mon_start'])){
				$what = 11;
			}else if(!empty($wheres['mon_end']) && !empty($wheres['mon_start'])){
				$arr1 = explode('-',$wheres['mon_end']);
				$arr2 = explode('-',$wheres['mon_start']);
				if($arr1['0']==$arr2['0']){
					$what = $arr1['1']-$arr2['1'];
				}else if($arr1['0']>$arr2['0']){
					$what = $arr1['1']+12-$arr['1'];
				}
			}else  if(!empty($wheres['mon_end']) && empty($wheres['mon_start'])){
				$what = 11;
			}else if(empty($wheres['mon_end']) && !empty($wheres['mon_start'])){
				$wheres['mon_end'] =date('Y-m',time());
				$arr1 = explode('-',$wheres['mon_end']);
				$arr2 = explode('-',$wheres['mon_start']);
				if($arr1['0']==$arr2['0']){
					$what = $arr1['1']-$arr2['1'];
				}else if($arr1['0']>$arr2['0']){
					$what = $arr1['1']+12-$arr['1'];
				}
			}
			
			$old_where = $wehre;
			for($i=$what;$i>=0;$i--){
				$where = $old_where;
				$start = date("Y-m",strtotime("-$i month"))."-01 00:00:00";
				$time = date("Y-m",strtotime("-$i month"))."-31 23:59:59";
				
				if(!empty($wheres['mon_start'])){
					$where.=" and order.finish_time >= '".$start."'";
				}
				
				if(!empty($wheres['mon_end'])){
					$where.=" and order.finish_time <= '".$time."'";
				}
					/*
				$where.=" and order.finish_time >= '".$start."'";
				$where.=" and order.finish_time <= '".$time."'";*/
				$information_list[$i]['total_price'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_price');
				$information_list[$i]['total_cost'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_cost');
				$information_list[$i]['finish_time'] = date("Y-m",strtotime("-$i month"));
				
				$information_list[$i]['profit'] = $information_list[$i]['total_cost']/(($information_list[$i]['total_price']-$information_list[$i]['total_cost'])*$exchange_rate['usd']);
				
				$information_list[$i]['count'] = count($information_list[$i]['total_price']);
				
				
				$arr=explode('.',$information_list[$i]['profit']);
		
				$num1=substr($arr[1],0,2);
				$str = $arr[0].'.'.$num1;
				$information_list[$i]['percentage'] = ($str*100).'%';
				if($wheres['team'] && empty($wheres['team_members'])){
						$information_list[$i]['team_name'] = $wheres['team'];
					}else if($wheres['team_members']){
						$information_list[$i]['team_name'] = $wheres['team_members'];
					}else{
						$information_list[$i]['team_name'] = "星爱";
					}
			}
		}else if($wheres['cycle']==1){
				$old_where = $wehre;
				if(!empty($start) && !empty($end)){
					$what = $end-$start;
				}else if(empty($start) && empty($end)){
				  	$what = 5;
			  	}else{
				  	if(empty($end)){
						$end = date("Y",time());
					 }
					 if(empty($start)){
						$start =  date("Y",strtotime("-7 year"));
					 }
						$what = $end-$start;
				}
					
				for($i=$what;$i>=0;$i--){
					$where = $old_where;
					
					$start = date("Y",strtotime("-$i year"))."-01-01 00:00:00";
					echo $start;die;
					$time = date("Y",strtotime("-$i year"))."-12-31 23:59:59";
					if(!empty($wheres['start'])){
						$where.=" and order.finish_time >= '".$start."'";
					}
				    
					if(!empty($wheres['end'])){
						$where.=" and order.finish_time <= '".$time."'";
					}
					
					$information_list[$i]['total_price'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_price');
					$information_list[$i]['total_cost'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_cost');
					$information_list[$i]['finish_time'] = date("Y",strtotime("-$i year"));
					
					$information_list[$i]['profit'] = $information_list[$i]['total_cost']/(($information_list[$i]['total_price']-$information_list[$i]['total_cost'])*$exchange_rate['usd']);
					
					$information_list[$i]['count'] = count($information_list[$i]['total_price']);
					
					
					$arr=explode('.',$information_list[$i]['profit']);
			
					$num1=substr($arr[1],0,2);
					$str = $arr[0].'.'.$num1;
					$information_list[$i]['percentage'] = ($str*100).'%';
					
					if($wheres['team'] && empty($wheres['team_members'])){
						$information_list[$i]['team_name'] = $wheres['team'];
					}else if($wheres['team_members']){
						$information_list[$i]['team_name'] = $wheres['team_members'];
					}else{
						$information_list[$i]['team_name'] = "星爱";
					}
				}
				
		}else{
			$information_list[$i]['total_price'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_price');
			$information_list[$i]['total_cost'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_cost');
			$information_list[$i]['finish_time'] = "All Time";
			
			$information_list[$i]['profit'] = $information_list[$i]['total_cost']/(($information_list[$i]['total_price']-$information_list[$i]['total_cost'])*$exchange_rate['usd']);
			
			$information_list[$i]['count'] = count($information_list[$i]['total_price']);
			
			
			$arr=explode('.',$information_list[$i]['profit']);
	
			$num1=substr($arr[1],0,2);
			$str = $arr[0].'.'.$num1;
			$information_list[$i]['percentage'] = ($str*100).'%';
			
			if($wheres['team'] && empty($wheres['team_members'])){
				$information_list[$i]['team_name'] = $wheres['team'];
			}else if($wheres['team_members']){
				$information_list[$i]['team_name'] = $wheres['team_members'];
			}else{
				$information_list[$i]['team_name'] = "星爱";
			}
		}
		
		 
		 $count['count']=count($information_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['information_list']=$information_list;
		 $result['count']=$count;
		 return $result;
		 
	}
	
	
	//数据管理列表页
	public function information_show($pageNo,$pageSize)
	{    
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $team_list = M('team')->field('team_name,team_members')->select();
		 $time = date("Y-m",time());
		 $arr = explode('-',$time);
		 $information_list = array();
		 $exchange_rate = M('exchange_rate')->find();
		for($i=0;$i<6;$i++){
			$start = date("Y-m",strtotime("-$i month"))."-01 00:00:00";
			$time = date("Y-m",strtotime("-$i month"))."-31 23:59:59";
			
		 	$where = " order.is_finish = '1' and order.finish_time>='$start' and order.finish_time<='$time'";
			$information_list[$i]['total_price'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_price');
			
			$information_list[$i]['total_cost'] = M('order')->join(' left join order_info on order_info.order_number = order.order_number')->where($where)->SUM('total_cost');
			$information_list[$i]['finish_time'] = date("Y-m",strtotime("-$i month"));
			
			$information_list[$i]['profit'] = $information_list[$i]['total_cost']/(($information_list[$i]['total_price']-$information_list[$i]['total_cost'])*$exchange_rate['usd']);
			
			$information_list[$i]['count'] = count($information_list[$i]['total_price']);
			
			
			$arr=explode('.',$information_list[$i]['profit']);
	
			$num1=substr($arr[1],0,2);
			$str = $arr[0].'.'.$num1;
			$information_list[$i]['percentage'] = ($str*100).'%';
			$information_list[$i]['team_name'] = "星爱";
		}
		 
		 $count['count']=count($information_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['information_list']=$information_list;
		 $result['count']=$count;
		 return $result;
		 
	}
	
	/*public function information_show($pageNo,$pageSize)
	{    
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $team_list = M('team')->field('team_name,team_members')->select();
		 $exchange_rate = M('exchange_rate')->find();
		 foreach($team_list as $key=>$val){
			 $arr = explode("|",$val['team_members']);
			 foreach($arr as $k=>$v){
				$list = M('order')->where(array('salesman'=>$v,'is_finish'=>'1'))->select();
				$team_list[$key]['count'] = count($list);
				foreach($list as $kk=>$vv){
					$team_list[$key]['total_price'] = M('order_info')->where(array('saleaman'=>$vv['saleaman'],'sales_return'=>'',status=>"4"))->SUM('total_price');
					$team_list[$key]['total_cost'] = M('order_info')->where(array('saleaman'=>$vv['saleaman'],'sales_return'=>'',status=>"4"))->SUM('total_cost');
					
					$team_list[$key]['profit'] = $team_list[$key]['total_cost']/(($team_list[$key]['total_price']-$team_list[$key]['total_cost'])*$exchange_rate['usd']);
					$arr=explode('.',$team_list[$key]['profit']);
			
					$num1=substr($arr[1],0,2);
					$str = $arr[0].'.'.$num1;
					$team_list[$key]['percentage'] = ($str*100).'%';
			
				}
				
			}
		}
		var_dump($team_list);
		 $count['count']=count($team_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['team_list']=$team_list;
		 $result['count']=$count;
		 return $result;
		 
	}*/
	
}
?>